using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using stsdev.UserInterface.Common;

namespace stsdev {
    public partial class SelectSolutionType : Form {

        public SelectSolutionType() {
            InitializeComponent();
        }

        private void SelectSolutionType_Load(object sender, EventArgs e) {
            // load default folder path 
            RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("TargetFolder");
            object defaultFolder = defaultFolderKey.GetValue("");
            if (defaultFolder != null && Directory.Exists(defaultFolder.ToString())) {
                txtParentDirectory.Text = defaultFolder.ToString();
            } else {
                txtParentDirectory.Text = @"c:\";
            }
            // load default solution version type
            RegistryKey defaultSolutionVersionType = Application.UserAppDataRegistry.CreateSubKey("SolutionVersionType");
            object solutionVersionType = defaultSolutionVersionType.GetValue("");
            if (solutionVersionType != null) {
                switch (solutionVersionType.ToString()) {
                    case "VS2005":
                        radVS2005.Checked = true;
                        break;
                    case "VS2008_30":
                        radVS2008_30.Checked = true;
                        break;
                    case "VS2008_35":
                        radVS2008_35.Checked = true;
                        break;
                }
                // Added 4/3/2009 - bphillips
                // Hide/show checkboxes
                SolutionVersionType_Changed(null, EventArgs.Empty);

                // Added 4/3/2009 - bphillips
                // Load default feature image
                RegistryKey defaultFeatureImage = Application.UserAppDataRegistry.CreateSubKey("ImageFile");
                object featureImage = defaultFeatureImage.GetValue(string.Empty);
                if (featureImage != null) {
                    featureImageTextBox.Text = featureImage.ToString();
                }

                // Added 20090403 - bphillips - Allow the user to select an app pool.
                RegistryKey defaultAppPool = Application.UserAppDataRegistry.CreateSubKey("AppPool");
                object appPool = defaultAppPool.GetValue(string.Empty);
                if (appPool != null) {
                    appPoolTextBox.Text = appPool.ToString();
                }
                // End of Added 20090403 - bphillips 

                // Added 20090403 - bphillips - Add support for IIS7 or later.
                RegistryKey defaultIISVersionType = Application.UserAppDataRegistry.CreateSubKey("IISVersionType");
                object iisVersionType = defaultIISVersionType.GetValue(string.Empty);
                if (iisVersionType != null) {
                    switch ((IISVersionEnum) Enum.Parse(typeof(IISVersionEnum), iisVersionType.ToString())) {
                        case IISVersionEnum.IIS6:
                            iis6RadioButton.Checked = true;
                            break;
                        case IISVersionEnum.IIS7orLater:
                            iis7RadioButton.Checked = true;
                            break;
                    }
                }
                // End of Added 20090403 - bphillips 

                // update UI
                EnableCreateButton();
                // set txtSolutionName as control with focus
                txtSolutionName.Focus();
            }

            //David McWee 6-9-2008
            //Update to use a drop down list instead of a simple textbox, this make the UI match Visual Studio Style.
            //load the New and Browse text into the signing key combo box
            cbSigningKey.Items.Insert(int.Parse(Properties.Resources.NewSigningKeyIndex), Properties.Resources.NewSigningKeyText);
            cbSigningKey.Items.Insert(int.Parse(Properties.Resources.BrowseSigningKeyIndex), Properties.Resources.BrowseSigningKeyText);

            //David McWee 6-9-2008
            //Load the previous keys into the dropdown list to allow the user access to multiple previously used keys.
            RegistryKey defaultSigningKeyFileKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
            object solutionSigningKeyFile = defaultSigningKeyFileKey.GetValue("");
            if (solutionSigningKeyFile != null && File.Exists(solutionSigningKeyFile.ToString())) {
                int cbSelectIndex = cbSigningKey.Items.Add(solutionSigningKeyFile.ToString());
                cbSigningKey.SelectedIndex = cbSelectIndex;
            } else
                cbSigningKey.Text = "";
        }

        public string GetSolutionName() {
            return txtSolutionName.Text;
        }

        public string GetParentDirectory() {
            return txtParentDirectory.Text;
        }

        public string GetSigningKeyFile() {
            //David McWee 6/9/2008 - Use the Dropdown list to get the KeyFile
            return cbSigningKey.Text;
        }

        // Added 20090403 - bphillips - Allow the user to select a different feature image.
        public string GetImageFile() {
            return featureImageTextBox.Text;
        }

        // Added 20090403 - bphillips - Allow the user to select a different app pool.
        public string GetAppPool() {
            return appPoolTextBox.Text;
        }

        // Added 20090403 - bphillips - Add support for IIS7 or later.
        public IISVersionEnum GetIISVersion() {
            if (iis7RadioButton.Checked){
                return IISVersionEnum.IIS7orLater;
            }

            return IISVersionEnum.IIS6;
        }

        // Added 4/3/2009 - bphillips - Enable ASP.Net Designer support
        public bool GetEnableASPNetSupport() {
            return enableASPNetCheckBox.Checked;
        }

        // Added 4/3/2009 - bphillips - Enable WF Designer support
        public bool GetEnableWFSupport() {
            return enableWFCheckBox.Checked;
        }

        public ISolutionProvider GetSelectedProvider() {
            return (ISolutionProvider) lstSolutionTypes.SelectedItem;
        }

        public SolutionVersionTypeEnum GetSolutionVersionType() {
            if (radVS2008_30.Checked)
                return SolutionVersionTypeEnum.VS2008_30;
            if (radVS2008_35.Checked)
                return SolutionVersionTypeEnum.VS2008_35;
            else
                return SolutionVersionTypeEnum.VS2005;
        }

        public void InitializeSolutionProviders(IList<ISolutionProvider> SolutionProviders) {
            lstSolutionTypes.DisplayMember = "Title";
            foreach (ISolutionProvider SolutionProvider in SolutionProviders) {
                lstSolutionTypes.Items.Add(SolutionProvider);
            }
            lstSolutionTypes.SelectedIndex = 0;
        }

        private void cmdChangeParentDirectory_Click(object sender, EventArgs e) {
            dlgFolderBrowser.SelectedPath = txtParentDirectory.Text;
            DialogResult result = dlgFolderBrowser.ShowDialog();
            if (result == DialogResult.OK) {
                txtParentDirectory.Text = dlgFolderBrowser.SelectedPath;
                RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("TargetFolder");
                defaultFolderKey.SetValue("", dlgFolderBrowser.SelectedPath);

            }
        }

        //David McWee 6/9/2008 - Removed in favor of HandleSigningKeyIndexChanged event handler
        //private void cmdChangeSigningKey_Click(object sender, EventArgs e) {
        //  SelectSigningKey dlg = new SelectSigningKey();
        //  DialogResult result = dlg.ShowDialog();
        //  if (result == DialogResult.OK) {
        //    txtSigningKey.Text = dlg.SigningKeyFile;
        //  }
        //}

        private void SolutionVersionType_Changed(object sender, EventArgs e) {
            RegistryKey defaultSolutionVersionType = Application.UserAppDataRegistry.CreateSubKey("SolutionVersionType");
            if (radVS2005.Checked)
                defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2005.ToString());
            if (radVS2008_30.Checked)
                defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2008_30.ToString());
            if (radVS2008_35.Checked)
                defaultSolutionVersionType.SetValue("", SolutionVersionTypeEnum.VS2008_35.ToString());

            // Added 20090403 - bphillips - Added support for ASP.Net and WF design-time
            if (radVS2008_30.Checked || radVS2008_35.Checked) {
                enableASPNetCheckBox.Visible = true;
                enableWFCheckBox.Visible = true;

                enableASPNetCheckBox.Checked = true;
                enableWFCheckBox.Checked = true;
            } else {
                enableASPNetCheckBox.Visible = false;
                enableWFCheckBox.Visible = false;

                enableASPNetCheckBox.Checked = false;
                enableWFCheckBox.Checked = false;
            }
            // End of Added 20090403 - bphillips
        }

        private void txt_TextChanged(object sender, EventArgs e) {
            EnableCreateButton();
        }

        private void EnableCreateButton() {
            if ((txtSolutionName.Text.Length > 0) &&
                 (txtParentDirectory.Text.Length > 0) &&
                 (cbSigningKey.Text.Length > 0)) {
                cmdCreateSolution.Enabled = true;
            } else {
                cmdCreateSolution.Enabled = false;
            }
        }

        //David McWee 6/9/2008 - Handle the user changing the dropdown lists selected index.  If the user selects the "New..." or "Browser..." provide the appropriate dialogs
        private void HandleSigningKeyIndexChange(object sender, EventArgs e) {
            ComboBox cb = sender as ComboBox;
            if (cb != null) {
                if (cb.SelectedIndex == int.Parse(Properties.Resources.NewSigningKeyIndex)) {
                    dlgSaveFileDialog.Title = "Create a new signing key file";
                    dlgSaveFileDialog.DefaultExt = "snk";
                    dlgSaveFileDialog.Filter = "Signing Key Files (*.snk)|*.snk";

                    dlgSaveFileDialog.FileName = "NewSigningKeyFile.snk";

                    DialogResult result = dlgSaveFileDialog.ShowDialog();
                    if (result == DialogResult.OK) {
                        string KeyFileName = dlgSaveFileDialog.FileName;
                        StrongNameKeyBuilder.CreateNew(KeyFileName);
                        int itemIndex = cbSigningKey.Items.Add(KeyFileName);

                        //We need to add the key file name to the solution somehow so we can add it to the project
                        //SolutionBuilder.KeyFileName = KeyFileName;

                        RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
                        defaultFolderKey.SetValue("", KeyFileName);

                        cb.SelectedIndex = itemIndex;
                    } else
                        cb.SelectedText = "";
                } else if (cb.SelectedIndex == int.Parse(Properties.Resources.BrowseSigningKeyIndex)) {
                    dlgOpenFileDialog.Title = "Select a signing key file";
                    dlgOpenFileDialog.DefaultExt = "snk";
                    dlgOpenFileDialog.Filter = "Signing Key Files (*.snk)|*.snk";

                    dlgOpenFileDialog.CheckFileExists = true;

                    if (!string.IsNullOrEmpty(txtParentDirectory.Text))
                        dlgOpenFileDialog.InitialDirectory = txtParentDirectory.Text;
                    else
                        dlgOpenFileDialog.InitialDirectory = @"c:\";


                    DialogResult result = dlgOpenFileDialog.ShowDialog();
                    if (result == DialogResult.OK) {
                        string KeyFileName = dlgOpenFileDialog.FileName;
                        int itemIndex = -1;

                        foreach (object item in cb.Items) {
                            if (item.ToString() == KeyFileName) {
                                itemIndex = cb.Items.IndexOf(item);
                                break;
                            }
                        }

                        if (itemIndex == -1)
                            itemIndex = cbSigningKey.Items.Add(KeyFileName);

                        RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("SigningKeyFile");
                        defaultFolderKey.SetValue("", KeyFileName);

                        cbSigningKey.SelectedIndex = itemIndex;
                    } else
                        cb.SelectedText = "";
                }
            } else
                throw new Exception("Sender was not the combo box!");

            EnableCreateButton();
        }

        // Added 20090403 - bphillips - Allow the user to select a different feature image.
        private void featureImageButton_Click(object sender, EventArgs e) {
            dlgOpenFileDialog.Title = "Select an image file";
            dlgOpenFileDialog.DefaultExt = "";
            dlgOpenFileDialog.Filter = "All Files (*.*)|*.*";

            dlgOpenFileDialog.CheckFileExists = true;

            if (!string.IsNullOrEmpty(txtParentDirectory.Text))
                dlgOpenFileDialog.InitialDirectory = txtParentDirectory.Text;
            else
                dlgOpenFileDialog.InitialDirectory = @"c:\";

            if (!string.IsNullOrEmpty(featureImageTextBox.Text)) {
                dlgOpenFileDialog.InitialDirectory = Path.GetDirectoryName(featureImageTextBox.Text);
                dlgOpenFileDialog.FileName = Path.GetFileName(featureImageTextBox.Text);
            } else {
                dlgOpenFileDialog.FileName = string.Empty;
            }

            DialogResult result = dlgOpenFileDialog.ShowDialog();
            if (result == DialogResult.OK) {
                string imageFileName = dlgOpenFileDialog.FileName;

                RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("ImageFile");
                defaultFolderKey.SetValue("", imageFileName);

                featureImageTextBox.Text = imageFileName;
            }
        }

        // Added 20090403 - bphillips - Allow the user to select an app pool.
        private void appPoolButton_Click(object sender, EventArgs e) {
            using (SelectAppPool dialog = new SelectAppPool()){
                if (!string.IsNullOrEmpty(appPoolTextBox.Text)) {
                    dialog.AppPool = appPoolTextBox.Text;
                }

                if (dialog.ShowDialog(this) == DialogResult.OK) {
                    appPoolTextBox.Text = dialog.AppPool;
                }
            }
        }

        // Added 20090403 - bphillips - Allow the user to select an app pool.
        private void appPoolTextBox_TextChanged(object sender, EventArgs e) {
            RegistryKey defaultFolderKey = Application.UserAppDataRegistry.CreateSubKey("AppPool");
            defaultFolderKey.SetValue("", appPoolTextBox.Text);
        }

        private void IISVersion_Changed(object sender, EventArgs e) {
            RegistryKey defaultIISVersionType = Application.UserAppDataRegistry.CreateSubKey("IISVersionType");
            if (iis6RadioButton.Checked) {
                defaultIISVersionType.SetValue("", IISVersionEnum.IIS6.ToString());
            } else if (iis7RadioButton.Checked) {
                defaultIISVersionType.SetValue("", IISVersionEnum.IIS7orLater.ToString());
            }
        }

    }
}